package com.yu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yu.bean.Dept;
import com.yu.service.DeptService;

@Controller
@RequestMapping("/dept")
public class DeptController {
	
	@Autowired
	private DeptService deptService;
	
	@RequestMapping("/getAll")
	public String getAll(Model model) {
		List<Dept> list = deptService.getAll();
		model.addAttribute("list", list);
		return "dept/showList";
	}
	
	@RequestMapping("/toAdd")
	public String toAdd() {
		return "dept/add";
	}
	
	@RequestMapping("/add")
	public String add(Dept dept) {
		deptService.add(dept);
		return "forward:/dept/getAll.do";
	}
	
	@RequestMapping("/delete")
	public String delete(int id) {
		deptService.delete(id);
		return "forward:/dept/getAll.do";
	}
	
	@RequestMapping("/toEdit")
	public String toEdit(Model model,int id) {
		Dept dept = deptService.getById(id);
		model.addAttribute("dept", dept);
		return "dept/edit";
	}
	
	@RequestMapping("/edit")
	public String edit(Dept dept) {
		deptService.update(dept);
		return "forward:/dept/getAll.do";
	}
}
